﻿using MediatR;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;
using System.ComponentModel.DataAnnotations;

namespace GetSlaughterItem.API.Application.Commands.Week02.Check_In
{
    public class CheckInShowHandler : IRequestHandler<CheckInShowCommand, Result>
    {
        IBaseRepositoryGet<CheckIn> CheckInRep;
        IBaseRepositoryGet<CheckInState> CheckInStateRep;
        IBaseRepositoryGet<SettlementType> SettlementTypeRep;
        IBaseRepositoryGet<Supplier> SupplierRep;

        public CheckInShowHandler(IBaseRepositoryGet<CheckIn> checkInRep, IBaseRepositoryGet<CheckInState> checkInStateRep, IBaseRepositoryGet<SettlementType> settlementTypeRep, IBaseRepositoryGet<Supplier> supplierRep)
        {
            CheckInRep = checkInRep;
            CheckInStateRep = checkInStateRep;
            SettlementTypeRep = settlementTypeRep;
            SupplierRep = supplierRep;
        }
        /// <summary>
        /// 进场登记的 显示 查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(CheckInShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = (from a in CheckInRep.GetAll()
                            join b in CheckInStateRep.GetAll()
                            on a.CheckInStateId equals b.CheckInStateId
                            join c in SettlementTypeRep.GetAll()
                            on a.SettlementTypeId equals c.SettlementTypeId
                            join d in SupplierRep.GetAll()
                            on a.SupplierId equals d.SupplierId
                            select new
                            {
                                a.CheckInId,
                                a.CheckInNo,
                                EnterTime = a.EnterTime,
                                EnterTime1 = a.EnterTime,
                                a.GoOutTime,
                                a.CarNo,
                                a.SupplierId,
                                a.SettlementTypeId,
                                a.ApproachNum,
                                a.QuarantineNo,
                                a.OriginAddr,
                                a.CheckInBatch,
                                a.Deliveryman,
                                a.Phone,
                                a.IdentityCard,
                                a.EnterRegistrar,
                                a.GoOutRegistrar,
                                a.QuarantineImg,
                                a.CheckInStateId,
                                a.Remark,
                                a.CheckInIsDel,
                                b.CheckInStateName,
                                b.CheckInStateIsDel,
                                c.SettlementTypeName,
                                c.SettlementTypeIsDel,
                                d.SupplierName,
                                d.SupplierIsDel
                            }).ToList();
                // 进场登记时间
                if (request.EnterTime != null)
                {
                    list = list.Where(x => x.EnterTime >= request.EnterTime).ToList();
                }
                // 供应商/养殖户 外健
                if (request.SupplierId > 0)
                {
                    list = list.Where(x => x.SupplierId == request.SupplierId).ToList();
                }
                // 结算类型 外键
                if (request.SettlementTypeId > 0)
                {
                    list = list.Where(x => x.SettlementTypeId == request.SettlementTypeId).ToList();
                }
                //进场登记状态
                if (request.CheckInStateId > 0)
                {
                    list = list.Where(x => x.CheckInStateId == request.CheckInStateId).ToList();
                }
                //总条数
                var totalcount = list.Count();
                //总页数
                var pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pagesize);
                //分页
                list = list.OrderBy(x => x.CheckInId).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                //返回元组
                var CheckInData = new
                {
                    totalcount,
                    pagecount,
                    list
                };

                //如果有数据就返回
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = CheckInData;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        } 
    }
}
